<?php

class Upload extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->helper('url');
        if (file_exists('application/controllers/install.php'))
            redirect('install');
        $this->load->library('session');
        $this->load->model('works_model');
        $this->load->model('college_model');
        $this->load->model('category_model');

        if (!($this->session->userdata['login'])) {
            redirect('poster_admin/login');
        }

    }

    function index() {
        // Set the config
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png|psd';
        $config['encrypt_name'] = 'TRUE';

        $this->load->library('upload', $config);
        $this->load->library('form_validation');

        $data['title'] = '提交作品';

        $this->form_validation->set_rules('title', '作品名', 'required|xss_clean');
        $this->form_validation->set_rules('realname', '真实姓名', 'required|xss_clean');
        $this->form_validation->set_rules('studentnum', '学号', 'trim|required|exact_length[12]|numeric');
        $this->form_validation->set_rules('college', '学院', 'required');
        $this->form_validation->set_rules('category', '参赛项目', 'required');
        $this->form_validation->set_rules('content', '作品介绍', 'required|xss_clean|htmlspecialchars|nl2br');
        //echo $this->upload;
        if ( $this->upload->do_upload()  === FALSE | $this->form_validation->run() === FALSE ) {

            if ($_SERVER['REQUEST_METHOD'] == 'POST')
                $data['error'] = $this->upload->display_errors();
            else
                $data['error'] = '';
            $data['college'] = $this->college_model->getAll();
            $data['category'] = $this->category_model->getAll();
            $this->load->view('templates/poster_admin/header', $data);
            $this->load->view('poster_admin/upload_form', $data);
            $this->load->view('templates/poster_admin/footer', $data);
        } else {
            $data = $this->upload->data();
            $this->works_model->insert($data['file_name']);
            redirect('/poster_admin/works/', 'refresh');
        }
    }
}
?>